/*
 * Copyright (C) 2015 UFreedom
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ufreedom.demo.transition;

import com.ufreedom.demo.util.AnimatorStateChangedListener;
import com.ufreedom.floatingview.transition.BaseFloatingPathTransition;
import com.ufreedom.floatingview.transition.FloatingPath;
import com.ufreedom.floatingview.transition.PathPosition;
import com.ufreedom.floatingview.transition.YumFloating;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Path;

/**
 * BeerFloating
 *
 * @author UFreedom
 * @since 2021-03-31
 */
public class BeerFloating extends BaseFloatingPathTransition {
    private static final float MXCOOR0 = -100;
    private static final float MYCOOR1 = -200;
    private static final float MXCOOR1 = 100;

    private static final float MYCOOR2 = 200;
    private static final float MXCOOR2 = -100;

    private static final int MANIMATORVALUE = 500;
    private static final long MTRANSLATEDURATION = 600L;

    private static final long MALPHASTARTDELAY = 550L;
    private static final long MALPHADURATION = 300L;

    /**
     * getFloatingPath
     *
     * @return FloatingPath
     */
    @Override
    public FloatingPath getFloatingPath() {
        Path path = new Path();
        path.rLineTo(MXCOOR0, 0);
        path.quadTo(0, MYCOOR1, MXCOOR1, 0);
        path.quadTo(0, MYCOOR2, MXCOOR2, 0);
        return FloatingPath.create(path, false);
    }

    /**
     * applyFloating
     *
     * @param yumFloating
     */
    @Override
    public void applyFloating(final YumFloating yumFloating) {
        AnimatorValue translateAnimator = new AnimatorValue();
        translateAnimator.setDuration(MTRANSLATEDURATION);
        translateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                PathPosition floatingPosition = getFloatingPosition(value * MANIMATORVALUE);
                yumFloating.setTranslationX(floatingPosition.x);
                yumFloating.setTranslationY(floatingPosition.y);
            }
        });
        AnimatorValue alphaAnimation = new AnimatorValue();
        alphaAnimation.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                yumFloating.setAlpha(1 - v);
            }
        });

        alphaAnimation.setStateChangedListener(new AnimatorStateChangedListener() {
            @Override
            public void onEnd(Animator animator) {
                yumFloating.clear();
            }
        });
        alphaAnimation.setDelay(MALPHASTARTDELAY);
        alphaAnimation.setDuration(MALPHADURATION);
        translateAnimator.start();
        alphaAnimation.start();
    }
}